﻿/****************************************************************************
*                                                                           *
* Copyright (c) Ionescu M. Marius. All rights reserved(2010).               *
*                                                                           *
****************************************************************************/

using System;
using System.Data;
using System.Web.UI;
using System.Web;

public class SessionPageBase : System.Web.UI.Page
{
    public static SessionPageBase thisPage;
	public SessionPageBase()
	{
	}

	public string UserID
	{
		get { return (string)(this.ViewState["UserID"] ?? ""); }
		set { this.ViewState["UserID"] = value; }
	}

	public string UserName
	{
		get { return (string)(this.ViewState["UserName"] ?? ""); }
		set { this.ViewState["UserName"] = value; }
	}

	private string PageGUID
	{
		get
		{
			if (ViewState["PageGUID"] == null)
				ViewState["PageGUID"] = Guid.NewGuid().ToString();
			return (string)ViewState["PageGUID"];
		}
	}

	public string GetSessionKey(string strTagName)
	{
		return this.PageGUID + "_" + strTagName;
	}

	public void SetSession(string strTagName, object oValue)
	{
		Session[GetSessionKey(strTagName)] = oValue;
	}

	public void ClearSession(string strTagName)
	{
		Session[GetSessionKey(strTagName)] = null;
	}

	public object GetSession(string strTagName)
	{
		return Session[GetSessionKey(strTagName)];
	}

    public void SetSessionGeneral(string strTagName, object oValue)
    {
        Session[strTagName] = oValue;
    }

    public void ClearSessionGeneral(string strTagName)
    {
        Session[strTagName] = null;
    }

    public object GetSessionGeneral(string strTagName)
    {
        return Session[strTagName];
    }

	protected DataSet BuildSessionDataSet(string sessionKey, string procedureName, string[] paramNames, object[] paramValues)
	{
		DataSet result = Hipocrate.DB.DBConnection.GetData(procedureName, paramNames, paramValues);

		foreach (DataTable dt in result.Tables)
			foreach (DataColumn dc in dt.Columns)
			{
				dc.ReadOnly = false;
				dc.AllowDBNull = true;
			}

		SetSession(sessionKey, result);
		return result;
	}

	public DataSet GetSessionDataSet(string strTagName)
	{
		return (DataSet)GetSession(strTagName);
	}

	public void SetMessage(string strMessage, bool bError)
	{
		if (bError)
			strMessage = "<PRE>" + Server.HtmlEncode(Tools.GetErrorMessage(strMessage)) + "</PRE>";
		strMessage = strMessage.ReplaceNewLineWithBR();
        ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "SetMessage", "DisplayMessage(\"" + strMessage.Replace(@"\", @"\\") + "\", '" + (bError ? "clsError" : "clsMessage") + "');", true);
	}

    public static void SetMessageStatic(string strMessage, bool bError)
    {
        if (bError)
            strMessage = "<PRE>" + HttpUtility.HtmlEncode(Tools.GetErrorMessage(strMessage)) + "</PRE>";
        strMessage = strMessage.ReplaceNewLineWithBR();
        ScriptManager.RegisterStartupScript(thisPage.Page, typeof(Page), "SetMessage", "DisplayMessage(\"" + strMessage.Replace(@"\", @"\\") + "\", '" + (bError ? "clsError" : "clsMessage") + "');", true);
    }
}
